package com.prs.crm.action.oa.report;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.domain.oa.Synod;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.oa.SynodService;

public class SynodReportAction extends ReportAction {

	private static final long serialVersionUID = -6177724271500496955L;
	
	private Date datestart;
	private Date dateend;
	private PaginationSupport pagination;
	private SynodService synodService;
	
	@Action("listSynodReport")
	public String listSynodReport(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Synod.class);
		if(datestart != null){
			criteria.add(Restrictions.ge("beginTime", datestart));
		}
		if(dateend != null){
			criteria.add(Restrictions.le("beginTime", dateend));
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSynodService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editSynodReport")
	public String editSynodReport(){
		return SUCCESS;
	}
	
	@Action(value="exportSynodReport", results = { @Result(name = "success"
		,type="stream",params={"contentType","${contentType}","inputName","inputStream"
		,"bufferSize","1024","contentDisposition","filename=${outfileName}"}) })
	public String exportSynodReport(){
		ReportTable table = new ReportTable("会议报表");
		table.addCell(new ReportCell("会议编号","id"));
		table.addCell(new ReportCell("主题","title"));
		table.addCell(new ReportCell("类型","type.name"));
		table.addCell(new ReportCell("会议室","synodRoom.name"));
		table.addCell(new ReportCell("开始时间","beginTime"));
		table.addCell(new ReportCell("结束时间","endTime"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Synod.class);
		if(datestart != null){
			criteria.add(Restrictions.ge("beginTime", datestart));
		}
		if(dateend != null){
			criteria.add(Restrictions.le("beginTime", dateend));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table, new ExportConfig(),this.getExportType()));
		return SUCCESS;
	}
	
	public Date getDatestart() {
		return datestart;
	}
	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}
	public Date getDateend() {
		return dateend;
	}
	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public SynodService getSynodService() {
		return synodService;
	}
	public void setSynodService(SynodService synodService) {
		this.synodService = synodService;
	}
	
}
